const fs = require('fs');
const child = require('child_process')

var sourceFolder = __dirname + "/Protos/";
var targetFolder = __dirname + "/Proto2CSharp/";
var exePath = 'protoc.exe';

var files = fs.readdirSync(sourceFolder);

files.forEach(file => {
    if (!file.endsWith('.proto'))
        return false;

    console.log("开始转换", file);

    try {

        var args = `${exePath} -I=${sourceFolder}/ tmp --csharp_out=${targetFolder} --csharp_opt=namespace_name=ShiohoNet --provider_out=${targetFolder} --paser_out=${targetFolder} --controller_out=${targetFolder}`;
        child.exec(args, (err, stdout, stderr) => {
            if (err) {
                console.error("转换失败", err, stderr);
                return;
            }

            console.log(stdout);
            console.log("转换完成", file);
        });

    } catch (e) {
        console.error("转换失败", e);
    }
});